﻿Imports System.Net.Mail
Imports DataAcessLayer

Partial Public Class WebForm8
    Inherits System.Web.UI.Page

    Dim DAL As New DAL

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Message.Visivel = False

        If Not Page.IsPostBack Then

        End If

    End Sub

    Protected Function EnviaMail(ByVal _from As String, _
                            ByVal _to As String, _
                            ByVal _assunto As String, _
                            ByVal _body As String) As Boolean
        Try

            Dim _mMsg As New MailMessage
            Dim _adress As New MailAddress(_from)
            Dim _para As New MailAddress(_to)
            Dim _mSMTP As New SmtpClient("HERMES.cofidis.pt")

            With _mMsg
                .From = _adress
                .To.Add(_para)
                .Subject = _assunto
                .Body = _body
                .IsBodyHtml = True
                .Priority = MailPriority.Normal
            End With

            _mSMTP.Send(_mMsg)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Protected Function EnvioMassMails() As Boolean

        Try

            Dim adress As New MailAddress("jpgoncalves@cofidis.pt")
            Dim mSMTP As New SmtpClient("HERMES.cofidis.pt")
            'Dim lista As New List(Of MailAddress)

            Dim listaEmail = DAL.ObterEmailsUltimoJogo

            'For Each _mail In listaEmail

            '    lista.Add(New MailAddress(_mail.Email))
            'Next

            Dim sTemp As New StringBuilder()

            sTemp.Append("<table>")
            sTemp.Append("<tr><td>MVP da Ultima Semana Atribuido!</td></tr>")
            sTemp.Append("<tr><td>Toca a votar no MVP desta semana!</td></tr>")
            sTemp.Append("<tr><td>http://lassomption/CromosdaBola/</td></tr>")

            For Each m In listaEmail

                Dim mMsg As New MailMessage

                With mMsg
                    .From = adress
                    .To.Add(m.Email)
                    .Subject = "Site Actualizado!"
                    .Body = sTemp.ToString
                    .IsBodyHtml = True
                    .Priority = MailPriority.Normal
                End With

                mSMTP.Send(mMsg)

                mMsg.Dispose()

            Next

            Return True

        Catch ex As Exception
            Return False
        End Try
    End Function

    Protected Sub lnkEnviarMail_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkEnviarMail.Click

        If EnviaMail("cromosdabola@cofidis.pt", txtPara.Text, txtAssunto.Text, txtBody.Text) Then

            Message.FeedbackUserMessage("Email enviado com Sucesso!", TipoMensagem.Sucesso)
        Else

            Message.FeedbackUserMessage("Ocorreu um erro ao enviar email", TipoMensagem.Erro)
        End If

    End Sub

    Protected Sub lnkEnvioMassa_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkEnvioMassa.Click

        If EnvioMassMails() Then

            Message.Visible = True
            Message.FeedbackUserMessage("Emails enviados com Sucesso!", TipoMensagem.Sucesso)
        Else

            Message.Visible = True
            Message.FeedbackUserMessage("Ocorreu um erro ao enviar emails", TipoMensagem.Erro)
        End If

    End Sub

End Class